<!-- 客服列表信息 -->
<template>
  <div class="table">
    <el-tabs v-model="activeName" @tab-click="handleChangeTabs">
      <el-tab-pane label="客服列表" name="one">
        <SearchTable :selectCustomerClass="selectCustomerClass" ref="searchTableRef" v-model:formStatus="formStatus" @handleAdd="handleAdd" @handleViewEdit="handleViewEdit" />
      </el-tab-pane>
      <el-tab-pane label="客服详情" name="two">
        <FormTab :selectCustomerClass="selectCustomerClass" v-model:formStatus="formStatus" v-model:form="form" @handleInitData="clearData" @handleSuccess="handleSuccess" @handleCusCategoryRefresh="handleGetCustomerServiceCategoryInfo" />
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script setup lang="ts" name="customerServicList">
import SearchTable from "@/components/configInfo/customer/customerServicList/SearchTable.vue";
import FormTab from "@/components/configInfo/customer/customerServicList/FormTab.vue";
import { getCustomerServiceCategoryInfo } from "@/utils/api/customerServiceCategory.js";
import type { TabsMode, ViewMode, ViewModeFilter } from "@/types/common";
import type { ICustomer, ISearchCustomer } from "@/types/customerServicClass";
import type { ICustomerList, ICustomerListDTO } from "@/types/customerServicList";
import type { TabsPaneContext } from "element-plus";

/* 表格状态 */
const formStatus = ref<ViewMode>("none");
/* 标签页切换 */
const activeName = ref<TabsMode>("one");
const selectCustomerClass = ref<Array<ICustomer>>([]);
/* 表格子组件的引用 */
const searchTableRef = useTemplateRef("searchTableRef");

const form = reactive<ICustomerListDTO>({
  id: 0,
  customer_service_category_id: "",
  name: "",
  mobile: "",
  sort: 0
});

/* 处理切换标签页 */
function handleChangeTabs(tab: TabsPaneContext) {
  if (tab.props.name === "two") {
    formStatus.value = "edit";
  } else if (tab.props.name === "one") {
    clearData();
  }
}

/* 清除数据 */
function clearData() {
  activeName.value = "one";
  formStatus.value = "none";
}

/* 处理修改成功 */
function handleSuccess() {
  searchTableRef.value?.getTableInfo();
}

/* 点击添加按钮 */
function handleAdd(code: ViewMode) {
  formStatus.value = code;
  activeName.value = "two";
}
/* 点击编辑按钮 */
function handleViewEdit(row: ICustomerList, code: ViewModeFilter) {
  formStatus.value = code;
  activeName.value = "two";
  /* nextTick 解决重置该表单项 初始化的数据问题 */
  nextTick(() => {
    form.id = row.id;
    form.customer_service_category_id = row.customer_service_category_id;
    form.name = row.name;
    form.mobile = row.mobile;
    form.sort = row.sort;
  });
}

/* 请求：获取表格中数据 */
function handleGetCustomerServiceCategoryInfo() {
  getCustomerServiceCategoryInfo<Array<ICustomer>, ISearchCustomer>()
    .then(res => {
      selectCustomerClass.value = res.result;
    })
    .finally(() => {});
}

handleGetCustomerServiceCategoryInfo();
</script>

<style lang="scss" scoped></style>
